// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

const {
	isNull
} = require("util");

module.exports = {
	_before: function() { // 通用预处理器
		this.dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		this.dbCmd = this.dbJQL.command
		this.$ = this.dbCmd.aggregate
	},

	/**
	 * 获取报考资讯列表
	 */
	async getList(skip = 0, limit = 8, type = null) {
		// 构建查询条件
		let query = this.dbJQL.collection("consult").skip(skip).limit(limit).orderBy('createTime', 'desc')
		// 如果有 type 条件，添加过滤
		if (type) {
			query = query.where({
				type
			})
		}
		return await query.field("type,title,auth,createTime,view_count,like_count").get()
	},
	/* 获取咨询详情 */
	async information_detail(id) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		return dbJQL.collection('consult').doc(id).get({
			getOne: true
		})
	},
	async information_publish(params) {
		const dbJQL = uniCloud.databaseForJQL({
			clientInfo: this.getClientInfo()
		})
		let {
			_id,
			...data
		} = params
		let reference = dbJQL.collection('consult')
		if (_id) {
			return reference.doc(_id).update(data)
		} else {
			return reference.add(data)
		}
	},
	/**
	 * 获取报考资讯详情
	 */
	async getDetail(id) {
		let res = await this.dbJQL.collection("consult").aggregate().match({
				_id: id
			})
			.lookup({
				from: 'like',
				let: {
					article_id: "$_id"
				},
				pipeline: this.$.pipeline().match(
					this.dbCmd.expr(
						this.$.and([
							this.$.eq(['$article_type', 0]),
							this.$.eq(['$article_id', "$$article_id"]),
							this.$.eq(['$user_id', this.dbJQL.getCloudEnv('$cloudEnv_uid')])
						])
					)
				).count('count').done(),
				as: 'is_like'
			})
			.lookup({
				from: 'favorite',
				let: {
					article_id: "$_id"
				},
				pipeline: this.$.pipeline().match(
					this.dbCmd.expr(
						this.$.and([
							this.$.eq(['$article_type', 0]),
							this.$.eq(['$article_id', "$$article_id"]),
							this.$.eq(['$user_id', this.dbJQL.getCloudEnv('$cloudEnv_uid')])
						])
					)
				).count('count').done(),
				as: 'is_favorite'
			})
			.limit(1)
			.project({
				title: 1,
				auth: 1,
				createTime: 1,
				like_count: 1,
				content: 1,
				is_like: this.$.cond({
					if: this.$.gt([this.$.arrayElemAt(['$is_like.count', 0]), 0]),
					then: true,
					else: false
				}),
				is_favorite: this.$.cond({
					if: this.$.gt([this.$.arrayElemAt(['$is_favorite.count', 0]), 0]),
					then: true,
					else: false
				}),
			}).end()
		if (res.errCode == 0) {
			// 解析第一层
			res.data = res.data[0]
			// 获取review次数
			let result = await this.dbJQL.collection('review').where(
				`article_id == '${res.data._id}' && user_id == $cloudEnv_uid`).get()
			if (result.errCode == 0) {
				if (result.data.length > 0) {
					this.dbJQL.collection('review').doc(result.data[0]._id).update({
						create_date: this.dbJQL.getCloudEnv('$cloudEnv_now')
					})
				} else {
					this.dbJQL.collection('review').add({
						article_id: res.data._id
					})
				}
			}
		}
		return res
	}
}